Skip to content

Fix JT4 encoding to match the WSJT-X reference - #42

Merged
NT7S merged 1 commit into
masterfrom
fix/jt4-implementation
Nov 29, 2026
Merged

NT7S merged 1 commit into
masterfrom
fix/jt4-implementation

Conversation

@NT7S

@NT7S NT7S commented Nov 28, 2026

Copy link
Copy Markdown
Member

What this fixes

The JT4 encoder produced frames that do not match WSJT-X. Four separate defects in jt4_encode/jt4_merge_sync_vector:

  1. Wrong frame length. JT4_SYMBOL_COUNT was 207; a JT4 frame is 206 symbols.
  2. Bogus one-symbol shift. The encoder filled a 206-bit buffer and then shifted everything right by one (memmove(s + 1, s, JT4_BIT_COUNT) + a hardcoded s[0] = 0). That corrupted the frame and read one element past the end of the buffer.
  3. Truncated/garbled sync vector. The table had only 126 entries (a bad transcription) and disagreed with the reference from the very first entry.
  4. Wrong sync/data merge. Sync was placed in the MSB and data in the LSB.

What changed

  • JT4_SYMBOL_COUNT 207 → 206; buffer for the convolution is now uint8_t s[JT4_BIT_COUNT] (was [JT4_SYMBOL_COUNT]).
  • Removed the memmove + s[0] = 0 shift entirely.
  • jt4_merge_sync_vector now takes the prepped message so it can detect negative reports.
  • Sync table replaced with the full 207-entry table from WSJT-X gen4.f90 (npr, whose first element is inert), read as npr[i + 1] for i = 0..205. Regenerated programmatically from the Fortran data statement after the hand-typed paste proved corrupted; verified len == 207 and element-wise equal to the reference.
  • Merge is now tone = 2 * data + sync (data MSB, sync LSB) — the correct 4-FSK mapping, replacing sync + 2 * g with sync in the MSB.
  • Inverted sync for negative reports, per gen4.f90: if the message contains a - in the report field (0-indexed position ≥ 8, with a guard that a following character exists) followed by a digit 0–3, the sync bits are complemented.
  • Fixed the doc comment in jt4_encode (said JT9_SYMBOL_COUNT) and the example comment (said 207 symbols).

Verification

Bit-exact against a Python reimplementation of the WSJT-X gen4/encode4 pipeline (packing → K=32 convolutional encode → jt9_interleave → sync merge with the reference npr table), which itself was checked against the Fortran data statement:

Message Result
NT7S MATCH
G0ABC M0SUP MATCH
G0ABC M0SUP -05 MATCH
CQ DX MATCH
AAAAAAA -05 (inverted sync triggers) MATCH
AAAAAAA 05 MATCH

All 206 tones in {0,1,2,3} in every case.

Harness: 20/20 PASS, 0/20 checks detected a bug. Three new checks:

  • jt4_frame_length — 206 tones, all in {0..3} (would have caught the uninitialized read).
  • jt4_bit_exact — bit-exact 206-symbol frame for NT7S.
  • jt4_inverted_sync — bit-exact frame for AAAAAAA -05.

Example builds on all five CI targets: AVR Uno (28,438 B / 88%), SAMD MKR1000 (31,364 B / 11%), ESP32 (300,516 B / 22%), ESP8266 NodeMCU (253,492 B / 24% flash), Renesas Uno R4 WiFi (72,192 B / 27%).

Version bumped 1.4.2 → 1.4.3 (patch), README changelog and jt4_encode docs updated.

Note on scope

JT4 keeps the library's existing simplified 72-bit jt9_bit_packing rather than implementing WSJT-X's full packmsg type detection — consistent with how JT9 and JT65 are handled in this library. The frame structure, sync vector, tone mapping and inverted-sync behaviour now match the reference exactly.

The JT4 encoder produced frames that did not match WSJT-X. Four separate
problems:

- JT4_SYMBOL_COUNT was 207, but a JT4 frame is 206 symbols.
- The encoder filled a 206-bit buffer and then shifted it right by one
  symbol (memmove + a hardcoded s[0] = 0), which corrupted the frame and
  read one element past the end of the buffer.
- The sync vector table had only 126 entries (a truncated transcription)
  and stopped matching the reference from the first entry.
- The sync/data merge put sync in the MSB and data in the LSB.

Rewrite the merge step against WSJT-X gen4.f90: use the full 207-entry
npr table (with its inert leading element, indexing npr(2:) for the 206
symbols) and map each symbol as tone = 2*data + sync, i.e. data MSB,
sync LSB. Add the negative-report case from gen4: when a '-' sits in the
report field (1-indexed position >= 9) and is followed by a digit 0-3,
the sync vector is inverted.

Verified bit-exact against a Python reimplementation of the gen4/encode4
pipeline (same 72-bit packing as the library's other modes) for report,
non-report, and negative-report messages. Harness grows to 20 checks with
JT4 frame-length, bit-exact, and inverted-sync coverage. Library still
builds on AVR, SAMD, ESP32, ESP8266 and Renesas.
@NT7S

NT7S commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

Upstream verification (added after Test Lab's PASS)

In addition to Test Lab's independent check, I verified the implementation against the actual upstream WSJT-X source (lib/gen4.f90, lib/encode4.f90, lib/encode232.f90, lib/conv232.f90, lib/interleave4.f90, lib/jt4.f90):

  • Sync table: the 207-entry npr in lib/jt4.f90 is element-wise identical to the table in src/JTEncode.cpp (match=True).
  • Convolution: the library's convolve() (32-bit registers, 0xf2d05351/0xe4613c47 taps, XOR-of-all-32-bits parity) is numerically identical to upstream encode232 (nstate & poly → n ^= n>>16 → partab((n ^ n>>8) & 0xff)). I initially mis-implemented the upstream parity in Python; after fixing that, both produce bit-identical output for the same input (0 diffs over 206 symbols).
  • Interleave: the library's jt9i (206 entries) is exactly the upstream j0 table from interleave4.f90 (bit-reversal permutation, n <= 205 filter). d[jt9i[i]] = s[i] is the same operation as itmp(j0(i)) = id(i).
  • Inverted sync: the condition i1 >= 9 (1-indexed) → 0-indexed i1 >= 8, digit '0'..'3', matches gen4.f90 verbatim.
  • Merge: tone = 2*data + sync matches itone=2*itone + npr(2:); inverted case matches 2*itone + (1-npr(2:)).
  • Frame length: itone(206) / symbol(206) — 206 symbols ✓.

The one remaining difference is the message-packing stage: the library keeps its simplified jt9_bit_packing (5+5+3 chars, base 42, freeform) rather than upstream packmsg's full Type-detection, which is consistent with how JT9 and JT65 already work in this library and is a documented design choice — not something this PR changes or needs to change.

@NT7S
NT7S merged commit e042d73 into master Nov 29, 2026
7 checks passed
@NT7S
NT7S deleted the fix/jt4-implementation branch November 30, 2026 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant